package com.gobuy.canal.listener;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.gobuy.canal.config.RabbitMQConfig;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Gaozexi on 2020/6/7 13:25
 * Step by Step  and Stand on,  You Are The Best Investment!
 */

/**
 * 声明为canal的监听类
 */
@CanalEventListener
public class BusinessListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    /**
     *
     * @param eventType 当前操作的数据库的类型
     * @param rowData 当前操作数据库的数据
     */
    @ListenPoint(schema = "changgou_business",table="tb_ad")
    public void adUpdate(CanalEntry.EventType eventType,CanalEntry.RowData rowData){
        System.out.println("广告数据发生改变");
        //rowData.getBeforeColumnsList().forEach(column -> System.out.println("改变前的数据 = " + column
        // .getName()+"=====>"+column.getValue()));
    //rowData.getAfterColumnsList().forEach(column -> System.out.println("改变后的数据 = " + column
        // .getName()+"=====>"+column.getValue()));
        for (CanalEntry.Column column : rowData.getAfterColumnsList()) {
            if ("position".equals(column.getName())) {
                System.out.println("发送最新数据到MQ column  "+column.getName() +"   value  "+column.getValue());
                //发送消息到 rabbitMQ convertAndSend(exchange,routing-key,content)
                rabbitTemplate.convertAndSend("", RabbitMQConfig.AD_UPDATE_QUEUE,column.getValue());
            }
        }

    }
}
